 package server.commands;


import client.MapleCharacter;
import client.MapleClient;
import com.alibaba.druid.pool.DruidPooledConnection;
import database.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Modifier;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;


















 public class CommandProcessor {
/*  21 */   private static final Logger log = LogManager.getLogger(CommandProcessor.class.getName());
/*  22 */   private static final Map<String, CommandObject> commands = new LinkedHashMap<>();
/*  23 */   private static final Map<PlayerGMRank, Map<String, Set<String>>> overrideCommands = new LinkedHashMap<>();
    private static final HashMap<Integer, ArrayList<String>> commandList = new HashMap<>();


   public static void init() {
/*  27 */     commands.clear();
/*  28 */     overrideCommands.clear();
     try {
/*  30 */       Properties properties = new Properties();
/*  31 */       try(FileInputStream fileInputStream = new FileInputStream("config/command.properties"); 
/*  32 */           InputStreamReader streamReader = new InputStreamReader(fileInputStream, "UTF-8"); 
/*  33 */           BufferedReader bufferedReader = new BufferedReader(streamReader)) {
/*  34 */         properties.load(bufferedReader);
       }


/*  38 */       properties.forEach((o1, o2) -> {
             String key = (String)o1;

             String value = (String)o2;

             String[] split = key.split("\\.");
             if (split.length != 2) {
               return;
             }
             PlayerGMRank rank = PlayerGMRank.valueOf(split[0].toUpperCase());
             String defcommand = split[1].toLowerCase();
             if (defcommand.equals("op")) {
               return;
             }
             value = value.replace("，", ",");
             String[] arrayCommands = value.split(",");
             for (String command : arrayCommands) {
                ((Set)((Map)overrideCommands.computeIfAbsent(rank, (k) -> {
                    return new HashMap();
                })).computeIfAbsent(defcommand, (k) -> {
                    return new HashSet();
                })).add(command.toLowerCase());

                             }
           });
/*  58 */     } catch (Exception e) {
/*  59 */       e.printStackTrace();
/*  60 */       System.out.println("导入GM命令出现错误");
     }

/*  63 */     Class<?>[] CommandFiles = new Class[] { PlayerCommand.class, InternCommand.class, GMCommand.class, AdminCommand.class, DonatorCommand.class, SuperDonatorCommand.class, SuperGMCommand.class };









/*  73 */     for (Class<?> clasz : CommandFiles) {
       try {
/*  75 */         PlayerGMRank rankNeeded = (PlayerGMRank)clasz.getMethod("getPlayerLevelRequired", new Class[0]).invoke(null, (Object[])null);
/*  76 */         Class<?>[] a = clasz.getDeclaredClasses();
/*  77 */         for (Class<?> c : a) {
           try {
/*  79 */             if (!Modifier.isAbstract(c.getModifiers()) && !c.isSynthetic()) {
/*  80 */               boolean enabled; Object o = c.newInstance();

               try {
/*  83 */                 enabled = c.getDeclaredField("enabled").getBoolean(c.getDeclaredField("enabled"));
/*  84 */               } catch (NoSuchFieldException ex) {
/*  85 */                 enabled = true;
               }
/*  87 */               if (o instanceof CommandExecute && enabled) {
/*  88 */                 String command = c.getSimpleName().toLowerCase();
/*  89 */                 commands.put(c.getSimpleName().toLowerCase(), new CommandObject((CommandExecute)o, getReqGMLevel(c.getSimpleName().toLowerCase(), rankNeeded)));
/*  90 */                 ((Set<String>)((Map<String, Set<String>>)overrideCommands.computeIfAbsent(rankNeeded, k -> new HashMap<>()))
/*  91 */                   .computeIfAbsent(command, k -> new HashSet()))
/*  92 */                   .add(command);
               }
             }
/*  95 */           } catch (Exception ex) {
/*  96 */             log.error(ex);
           }
         }
/*  99 */       } catch (Exception ex) {
/* 100 */         log.error(ex);
       }
     }
   }

   private static int getReqGMLevel(String simpleName, PlayerGMRank defRank) {
/* 106 */     PlayerGMRank ret = defRank;
/* 107 */     for (Map.Entry<PlayerGMRank, Map<String, Set<String>>> entry : overrideCommands.entrySet()) {
/* 108 */       if (((Map)entry.getValue()).containsKey(simpleName)) {
/* 109 */         ret = (ret.ordinal() > ((PlayerGMRank)entry.getKey()).ordinal()) ? ret : entry.getKey();
       }
     }

/* 113 */     return ret.getLevel();
   }

   private static void sendDisplayMessage(MapleClient c, String msg, CommandType type) {
/* 117 */     if (c.getPlayer() == null) {
       return;
     }
/* 120 */     switch (type) {
       case NORMAL:
/* 122 */         c.getPlayer().dropMessage(6, msg);
         break;
       case TRADE:
/* 125 */         c.getPlayer().dropMessage(-2, "错误 : " + msg);
         break;
       case POKEMON:
/* 128 */         c.getPlayer().dropMessage(-3, "(..." + msg + "..)");
         break;
     }
   }


   public static void dropHelp(MapleClient c) {
    StringBuilder sb = new StringBuilder("命令列表: ");
    for (int i = 0; i <= c.getPlayer().getGMLevel(); i++) {
        if (commandList.containsKey(i)) {
            for (String s : commandList.get(i)) {
                if (s.equals("@TestMapTimer")) {
                    continue;
                }
                sb.append(s);
                sb.append(" ");
            }
        }
    }
    c.getPlayer().dropMessage(6, sb.toString());
   }

   public static boolean processCommand(MapleClient c, String line, CommandType type) {
/* 162 */     int gmLevel = c.getPlayer().getGMLevel();
/* 163 */     line = line.replace('！', '!');
/* 164 */     String[] splitted = line.split(" ");
/* 165 */     splitted[0] = splitted[0].toLowerCase();
/* 166 */     String command = splitted[0].substring(1, splitted[0].length());

/* 168 */     if (command.isEmpty()) {
/* 169 */       return false;
     }

/* 172 */     for (Map.Entry<PlayerGMRank, Map<String, Set<String>>> entry : overrideCommands.entrySet()) {
/* 173 */       if (gmLevel >= ((PlayerGMRank)entry.getKey()).getLevel()) {
/* 174 */         for (Map.Entry<String, Set<String>> commands : (Iterable<Map.Entry<String, Set<String>>>)((Map)entry.getValue()).entrySet()) {
/* 175 */           if (((Set)commands.getValue()).contains(command)) {
/* 176 */             command = commands.getKey();
           }
         }
       }
     }


/* 183 */     CommandObject co = CommandProcessor.commands.get(command);

/* 185 */     PlayerGMRank commandLevel = PlayerGMRank.getPlayerGMRankByCommandPrefix(line.charAt(0));

/* 187 */     if (commandLevel != null && commandLevel.getLevel() <= gmLevel) {
/* 188 */       if (gmLevel <= PlayerGMRank.SUPERDONATOR.getLevel()) {
/* 189 */         if (PlayerGMRank.isPlayerCommandPrefix(line.charAt(0)) || PlayerGMRank.isActivityCommandPrefix(line.charAt(0))) {
/* 190 */           if (co == null || co.getReqGMLevel() > gmLevel || co.getType() != type) {
/* 191 */             sendDisplayMessage(c, "输入的玩家命令不存在.", type);
/* 192 */             return true;
           }
           try {
/* 195 */             co.execute(c, splitted);
/* 196 */           } catch (Exception e) {
/* 197 */             sendDisplayMessage(c, "使用命令出现错误.", type);
/* 198 */             if (c.getPlayer().isGM()) {
/* 199 */               sendDisplayMessage(c, "错误: " + e, type);
/* 200 */               log.error(e);
             }
           }
/* 203 */           return true;
         }
/* 205 */       } else if (PlayerGMRank.isGMCommandPrefix(line.charAt(0)) || gmLevel >= commandLevel.getLevel()) {
/* 206 */         if (line.charAt(0) == '@') {
/* 207 */           co.execute(c, splitted);
/* 208 */           return true;
         }

/* 211 */         if (command.equals("help")) {
/* 212 */           dropHelp(c);
/* 213 */           return true;
         }

/* 216 */         if (co == null || co.getReqGMLevel() > gmLevel || co.getType() != type) {
/* 217 */           sendDisplayMessage(c, "输入的命令不存在.", type);
/* 218 */           return true;
         }

/* 221 */         if (c.getPlayer().getGMLevel() >= co.getReqGMLevel()) {
/* 222 */           int ret = 0;
           try {
/* 224 */             ret = co.execute(c, splitted);
/* 225 */           } catch (ArrayIndexOutOfBoundsException x) {
/* 226 */             sendDisplayMessage(c, "使用命令出错，该命令必须带参数才能使用: " + x, type);
/* 227 */           } catch (Exception e) {
/* 228 */             log.error(e);
           }
/* 230 */           if (ret > 0 && c.getPlayer() != null) {
/* 231 */             if (c.getPlayer().isGM()) {
/* 232 */               logCommandToDB(c.getPlayer(), line, "gmlog");
             } else {
/* 234 */               logCommandToDB(c.getPlayer(), line, "internlog");
             }
           }
         } else {
/* 238 */           sendDisplayMessage(c, "您的权限等级不足以使用次命令.", type);
         }
/* 240 */         return true;
       }
     }
/* 243 */     return false;
   }

   private static void logCommandToDB(MapleCharacter player, String command, String table) {
/* 247 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection();
/* 248 */         PreparedStatement ps = con.prepareStatement("INSERT INTO " + table + " (cid, name, command, mapid) VALUES (?, ?, ?, ?)")) {
/* 249 */       ps.setInt(1, player.getId());
/* 250 */       ps.setString(2, player.getName());
/* 251 */       ps.setString(3, command);
/* 252 */       ps.setInt(4, player.getMap().getId());
/* 253 */       ps.executeUpdate();
     }
/* 255 */     catch (SQLException ex) {
/* 256 */       log.error(ex);
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\server\commands\CommandProcessor.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */